package checkauto.camera.com;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import checkauto.camera.com.util.AppManager;
import checkauto.camera.com.util.ReturnData;
import checkauto.camera.com.util.SharedPreferencesHelper;

//识别Activity调用类及结果显示页面
public class BucardRunner extends Activity {
	public static final String TAG = "BucardRunner";
	public static final String PATH = Environment.getExternalStorageDirectory()
			.toString() + "/AndroidWT";
	private String selectPath;
	private String str = "";
	private EditText editResult;
	private int edit_file, bucardrunner, exception, exception1, exception2,
			exception6, takePic1, backIndex1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制横屏
		bucardrunner = this.getResources().getIdentifier("bucardrunner",
				"layout", this.getPackageName());
		setContentView(bucardrunner);

		// 调用识别
		initViews();
		AppManager.getAppManager().finishActivity(CameraActivity.class);
		readRecogResult();

	}

	/**
	 * 读取识别结果
	 */
	private void readRecogResult() {
		// 读识别返回值
		Bundle bun = getIntent().getBundleExtra("RecogValue");
		if (bun != null) {
			int ReturnAuthority = bun.getInt("ReturnAuthority", -100000);// 取激活状态
			int ReturnInitBUCard = bun.getInt("ReturnInitBuCard", -100000);// 取初始化返回值
			int ReturnRecogBuCard = bun.getInt("RecogReturn", -100000);// 取识别的返回值
			if (ReturnAuthority == 0 && ReturnInitBUCard == 0
					&& ReturnRecogBuCard == 0) {
				// System.out.println("接收结果");
				String imagepath = bun.getString("ReturnLPFileName");
				int authNum = bun.getInt("ReturnAuthority");
				ArrayList<HashMap<String, String>> list2 = new ArrayList<>();
				if (authNum == 0) {
					@SuppressWarnings("unchecked")
					ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) bun
							.getParcelableArrayList("list").get(0);
                    list2 = list;
					for (int i = 0; i < list.size(); i++) {
						HashMap<String, String> map = list.get(i);
						
						str += map.get("Name") + ":" + map.get("Val") + "\n";

					}
				}
				ReturnData.getDefault().getCallData().callData(list2,imagepath);
				finish();
//				editResult.setText(str);

			} else {
				String str = "";
				if (ReturnAuthority == -100000) {
					str = getString(exception) + ReturnAuthority;
				} else if (ReturnAuthority != 0) {
					str = getString(exception1) + ReturnAuthority;
				} else if (ReturnInitBUCard != 0) {
					str = getString(exception2) + ReturnInitBUCard;
				} else if (ReturnRecogBuCard != 0) {
					str = getString(exception6) + ReturnRecogBuCard;
				}
				Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
				finish();
//				editResult.setText("识别结果 :" + str + "\n");
			}
		}
	}

	/**
	 * 初始化布局
	 */
	private void initViews() {
		edit_file = this.getResources().getIdentifier("edit_file", "id",
				this.getPackageName());
		exception = this.getResources().getIdentifier("exception", "string",
				this.getPackageName());
		exception1 = this.getResources().getIdentifier("exception1", "string",
				this.getPackageName());
		exception2 = this.getResources().getIdentifier("exception2", "string",
				this.getPackageName());
		exception6 = this.getResources().getIdentifier("exception6", "string",
				this.getPackageName());
		takePic1 = this.getResources().getIdentifier("takePic", "id",
				this.getPackageName());
		backIndex1 = this.getResources().getIdentifier("backIndex", "id",
				this.getPackageName());

		editResult = (EditText) this.findViewById(edit_file);
		Button takePic = (Button) this.findViewById(takePic1);
		Button backIndex = (Button) this.findViewById(backIndex1);
		backIndex.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(BucardRunner.this, ImageChooser.class);
				BucardRunner.this.finish();
				startActivity(intent);
			}
		});
		takePic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent();
				intent.setClass(BucardRunner.this, CameraActivity.class);
				intent.putExtra("autocamera", SharedPreferencesHelper.getBoolean(
						getApplicationContext(), "isAutoRecog", false));
				BucardRunner.this.finish();
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == 10 && resultCode == RESULT_OK) {
			// 读识别返回值
			Bundle bun = data.getBundleExtra("GetRecogResult");
			setContentView(bucardrunner);
			initViews();
			int ReturnAuthority = bun.getInt("ReturnAuthority", -100000);// 取激活状态
			int ReturnInitBUCard = bun.getInt("ReturnInitBuCard", -100000);// 取初始化返回值
			int ReturnRecogBuCard = bun.getInt("RecogReturn", -100000);// 取识别的返回值
			if (ReturnAuthority == 0 && ReturnInitBUCard == 0
					&& ReturnRecogBuCard == 0) {
				// System.out.println("接收结果");
				int authNum = bun.getInt("ReturnAuthority");
				if (authNum == 0) {
					ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) bun
							.getParcelableArrayList("list").get(0);
					for (int i = 0; i < list.size(); i++) {
						HashMap<String, String> map = list.get(i);
						str += map.get("Name") + ":" + map.get("Val") + "\n";
					}
				}
				editResult.setText(str);

			} else {
				String str = "";
				if (ReturnAuthority == -100000) {
					str = getString(exception) + ReturnAuthority;
				} else if (ReturnAuthority != 0) {
					str = getString(exception1) + ReturnAuthority;
				} else if (ReturnInitBUCard != 0) {
					str = getString(exception2) + ReturnInitBUCard;
				} else if (ReturnRecogBuCard != 0) {
					str = getString(exception6) + ReturnRecogBuCard;
				}
				editResult.setText("识别结果 :" + str + "\n");
			}

		}
	}
}
